//Documentation complete
/**
 * @author Kazu
 * @version 2009FE09
 */

package problemDomain;

import problemDomain.ErrorBean.*;
import static problemDomain.ErrorBean.ErrorCode.*;
import static utilities.XMLTools.encodeEntities;

import java.util.Date;

/**
 * A patron who can order food and has a persistent login account.
 */
public class RegisteredPatronAccountBean extends PatronAccountBean {
	
	/* Constants */
	
	private static final long serialVersionUID = -1137015958244613004L;
	
	/* END Constants */
	
	
	/* Attributes */
	
	protected String username;
	protected String password;
	protected String email;
	protected String street;
	protected String city;
	protected String province;
	protected String postalCode;
	protected Date dateEntered;
	
	/* END Attributes */
	
	
	/* Constructors */
	
	/**
	 * Constructs a RegisteredPatronAccountBean object with default values.
	 */
	public RegisteredPatronAccountBean () {
		super();
		username = "";
		password = "";
		email = "";
		street = "";
		city = "";
		province= "";
		postalCode = "";
		dateEntered = null;
	}
	
	/**
	 * Constructs a RegisteredPatronAccountBean object with an ID.
	 * @param id the ID of the patron account
	 */
	public RegisteredPatronAccountBean (int id) {
		this();
		this.id = id;
	}
	
	/**
	 * Constructs a RegisteredPatronAccountBean object with specified values.
	 * @param id the <code>Patron</code> ID
	 * @param username The username of the <code>Patron</code>
	 * @param password The password of the <code>Patron</code>
	 * @param firstName The first name of the <code>Patron</code>
	 * @param lastName The last name of the <code>Patron</code>
	 * @param phone The phone number of the <code>Patron</code>
	 * @param email The email of the <code>Patron</code>
	 * @param street The street of the <code>Patron</code> address
	 * @param city The city of the <code>Patron</code> address
	 * @param province The province of the <code>Patron</code> address 
	 * @param postalCode The postal code of the <code>Patron</code> 
	 * @param dateEntered The <code>Date</code> this <code>Patron</code> was first registered
	 */
	public RegisteredPatronAccountBean (
			int id, String firstName,
			String lastName, String phone, String username, String password, String email, String street,
			String city, String province, String postalCode, Date dateEntered) {
		super(id, firstName, lastName, phone);
		this.username = username;
		this.password = password;
		this.email = email;
		this.street = street;
		this.city = city;
		this.province = province;
		this.postalCode = postalCode;
		this.dateEntered = dateEntered;
	}
	
	/**
	 * Constructs a RegisteredPatronAccountBean object with specified values, but no password.
	 * @param username The username of the <code>Patron</code>
	 * @param password The password of the <code>Patron</code>
	 */
	public RegisteredPatronAccountBean (String username, String password) {
		this();
		this.username = username;
		this.password = password;
	}
	
	/**
	 * Constructs a RegisteredPatronAccountBean object with specified values, but no password.
	 * @param id the <code>Patron</code> ID
	 * @param username The username of the <code>Patron</code>
	 * @param firstName The first name of the <code>Patron</code>
	 * @param lastName The last name of the <code>Patron</code>
	 * @param phone The phone number of the <code>Patron</code>
	 * @param email The email of the <code>Patron</code>
	 * @param street The street of the <code>Patron</code> address
	 * @param city The city of the <code>Patron</code> address
	 * @param province The province of the <code>Patron</code> address 
	 * @param postalCode The postal code of the <code>Patron</code> 
	 * @param dateEntered The <code>Date</code> this <code>Patron</code> was first registered
	 */
	public RegisteredPatronAccountBean (
			int id, String username, String firstName,
			String lastName, String phone, String email, String street,
			String city, String province, String postalCode, Date dateEntered) {
		this(id, firstName, lastName, phone, username, "", email, street, city, province, postalCode, dateEntered);
	}
	
	/* END Constructors */
	
	
	/* Accessors */
	
	/**
	 * Returns the username.
	 * @return the username
	 */
	public final String getUsername () {
		return username;
	}

	/**
	 * Returns the password
	 * @return the password
	 */
	public final String getPassword () {
		return password;
	}

	/**
	 * Returns the email.
	 * @return the email
	 */
	public final String getEmail () {
		return email;
	}
	
	/**
	 * Returns the street.
	 * @return the street
	 */
	public String getStreet () {
		return street;
	}
	
	/**
	 * Returns the city.
	 * @return the city
	 */
	public String getCity () {
		return city;
	}
	
	/**
	 * Returns the province.
	 * @return the province
	 */
	public String getProvince () {
		return province;
	}
	
	/**
	 * Returns the postal code.
	 * @return the postal code
	 */
	public String getPostalCode () {
		return postalCode;
	}
	
	/**
	 * Returns the date the patron was created.
	 * @return the date created
	 */
	public Date getDateEntered () {
		return dateEntered;
	}
	
	/* END Accessors */
	
	
	/* Mutators */
	
	/**
	 * Sets the username.
	 * @param username the username of the patron account
	 */
	public final void setUsername (String username) {
		this.username = username;
	}

	/**
	 * Sets the password.
	 * @param password the password of the patron account
	 */
	public final void setPassword (String password) {
		this.password = password;
	}
	
	/**
	 * Sets the email.
	 * @param email the email to set
	 */
	public void setEmail (String email) {
		this.email = email;
	}
	
	/**
	 * Sets the street.
	 * @param street the street to set
	 */
	public void setStreet (String street) {
		this.street = street;
	}
	
	/**
	 * Sets the city.
	 * @param city the city to set
	 */
	public void setCity (String city) {
		this.city = city;
	}
	
	/**
	 * Sets the province.
	 * @param province the province to set
	 */
	public void setProvince (String province) {
		this.province = province;
	}
	
	/**
	 * Sets the postal code.
	 * @param postalCode the postal code to set
	 */
	public void setPostalCode (String postalCode) {
		this.postalCode = postalCode;
	}
	
	/**
	 * Sets the date this patron was created.
	 * @param dateEntered the date created
	 */
	public void setDateEntered (Date dateEntered) {
		this.dateEntered = dateEntered;
	}
	
	/* END Mutators */
	
	
	/* (Bean) Methods */
	
	@Override
	public boolean isValid () throws ErrorBean {
		super.isValid();
		
		if (username != null && username.length() > 15)
			throw new ErrorBean(
				PATRON_USERNAME_TOO_LONG,
				username);
		
		if (password != null && password.length() > 15)
			throw new ErrorBean(ErrorCode.PATRON_PASSWORD_TOO_LONG,password);
		else if (password != null && password.length() < 6)
			throw new ErrorBean(
				PATRON_PASSWORD_TOO_SHORT);
		
		if (email != null && email.length() > 50)
			throw new ErrorBean(
				PATRON_EMAIL_INVALID,
				email);
		
		if (street != null && street.length() > 40)
			throw new ErrorBean(
				PATRON_STREET_INVALID,
				street);
		
		if (city != null && city.length() > 30)
			throw new ErrorBean(
				PATRON_CITY_INVALID,
				city);
		
		if (province != null && province.length() != 2)
			throw new ErrorBean(
				PATRON_PROVINCE_INVALID,
				province);
		
		if (postalCode != null && postalCode.length() != 6)
			throw new ErrorBean(
				PATRON_POSTAL_CODE_INVALID,
				postalCode);
		
		return true;
	}
	
	@Override
	public String toXML () {
		StringBuilder sb = new StringBuilder();
		sb.append("<registeredpatron>");
		sb.append("<id>" + id + "</id>");
		sb.append("<firstname>" + firstname + "</firstname>");
		sb.append("<lastname>" + lastname + "</lastname>");
		sb.append("<phone>" + phone + "</phone>");
		sb.append("<username>" + username + "</username>");
		sb.append("<password>" + password + "</password>");
		sb.append("<email>" + email + "</email>");
		sb.append("<street>" + street + "</street>");
		sb.append("<city>" + city + "</city>");
		sb.append("<province>" + province + "</province>");
		sb.append("<postalcode>" + postalCode + "</postalcode>");
		sb.append("<dateentered>" + dateEntered + "</dateentered>");
		sb.append("</registeredpatron>");
		return encodeEntities(sb.toString());
	}
	
	/* END (Bean) Methods */
	
	
	/* (Object) Methods */
	
	@Override
	public String toString () {
		StringBuilder sb = new StringBuilder();
		sb.append(super.toString());
		sb.append("Email: " + email + '\n');
		sb.append("Street: " + street + '\n');
		sb.append("City: " + city + '\n');
		sb.append("Province: " + province + '\n');
		sb.append("Postal Code: " + postalCode + '\n');
		sb.append("Date Entered: " + dateEntered + "\n\n");
		return sb.toString();
	}
	
	/* END (Object) Methods */
	
}
